package com.navinfo.platform.trip.common.arithmetic;

import com.navinfo.platform.trip.common.arithmetic.annotation.Arithmetic;
import com.navinfo.platform.trip.common.arithmetic.data.CommonData;
import com.navinfo.platform.trip.common.arithmetic.data.OuterStatisticData;
import com.navinfo.platform.trip.common.arithmetic.var.VarGasSharp;
import com.navinfo.platform.trip.common.arithmetic.enums.EventStatus;
import com.navinfo.platform.trip.common.arithmetic.enums.EventType;
import com.navinfo.platform.trip.common.arithmetic.exception.RangeAssert;
import com.navinfo.platform.trip.common.arithmetic.manager.AbstractArithmetic;
import com.navinfo.platform.trip.common.config.ConfigMap;
import com.navinfo.platform.trip.common.util.ArithmeticUtils;

/**
 * 激烈油门算法.<br/>
 * 依赖{@link Mileage}里程油耗算法<br/>
 * 输出：统计、事件<br/>
 */
@Arithmetic(name = "激烈油门算法")
public class GasSharp extends AbstractArithmetic<VarGasSharp> {
    private final  EventType EVENT_TYPE = EventType.sharpStepOnAccelerator;
    /**
     * 油门阀值
     */
    private double gasThreshold = ConfigMap.getDouble("Throt_Change_Threshold");
    /**
     * 是否激烈油门判断，两点间油门开度增加40%以上
     */
    private boolean isGasSharp(VarGasSharp varGasSharp) {
        if(varGasSharp.getLastAccelerator() != null){
            //当前点油门-上个点的油门>40%
            return (getCommonData().getAcceleratorPosition()- varGasSharp.getLastAccelerator()) > gasThreshold;
        }else{
            return false;
        }
    }

    @Override
    public void calculate() {
        logger.debug("===============激烈油门算法开始===========");

        //1.油门开合度有效值验证
        CommonData commonData = getCommonData();
        RangeAssert.acceleratorIsInTheRange(commonData.getAcceleratorPosition());

        VarGasSharp var = getObj();
        boolean preGasSharp = var.isPreGasSharp();
        boolean curGasSharp = isGasSharp(var);
        logger.debug("激烈油门算法，激烈油门状态：pre:{},current:{}.", preGasSharp, curGasSharp);

        //2.上一次和本次有激烈油门时，进行计算
        if (preGasSharp || curGasSharp) {
            EventStatus eventStatus = super.getEventStatus(preGasSharp, curGasSharp);
            if (EventStatus.START.equals(eventStatus)) {
                logger.debug("激烈油门算法，事件开始.");
                startEventData(EVENT_TYPE);
            } else if (EventStatus.IN.equals(eventStatus)) {
                logger.debug("激烈油门算法，事件中.");
                //累加统计数据
                calcVarGasSharp(var);
                //累加事件数据
                calcEventData(EVENT_TYPE);
            } else if (EventStatus.END.equals(eventStatus)) {
                logger.debug("激烈油门算法，事件结束.");
                //累加统计数据
                calcVarGasSharp(var);
                var.setNumber(var.getNumber() + 1);

                calcEventData(EVENT_TYPE);
                endEventData(EVENT_TYPE);
            }
        }

        //设置上一个点的状态，为下次计算使用
        var.setPreGasSharp(curGasSharp);
        var.setLastAccelerator(commonData.getAcceleratorPosition());
        logger.debug("===============激烈油门算法结束===========");
    }

    @Override
    public void collect(OuterStatisticData statisticDataOut) {
        VarGasSharp var = super.getObj();
        statisticDataOut.setSharpStepOnGasNumber(var.getNumber());
        statisticDataOut.setSharpStepOnGasMileage(ArithmeticUtils.mileageMToM(var.getMileage()));
        statisticDataOut.setSharpStepOnGasFuel(ArithmeticUtils.fuelMlToMl(var.getFuel()));
        statisticDataOut.setSharpStepOnGasDuration(ArithmeticUtils.timeMsToSecond(var.getDuration()));
    }

    /**
     * 累加统计的里程、油耗、时长
     */
    private void calcVarGasSharp(VarGasSharp var) {
        CommonData commonData = getCommonData();
        var.addMileage( commonData.getDffMileage());
        var.addFuel( commonData.getDffFuel());
        var.addDuration(commonData.getDffTime());
    }
}
